package com.woshua.tabmodel.indicate;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

/*
 * 自定义指示器
 * */
public class ViewPageIndicate extends LinearLayout {

    public static final float RADIO_TRIANGLE_WIDTH = 1 / 6f;
    private int TriangleHeight;//三角形的高度
    private int TriangleWidth;//三角的宽度
    //界面显示数量
    private int mVisibleCount = 3;//默认显示的数量
    private Paint mPaint;
    private Path mPath;
    private int mInitTriangleX;
    private int mTriangleX;

    private OnPageChangeListener listener;

    public ViewPageIndicate(Context context) {
        this(context, null);
    }

    public ViewPageIndicate(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setPathEffect(new CornerPathEffect(3));
    }

    /*
     *绘制界面操作
     * */
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        canvas.save();
        canvas.translate(mInitTriangleX + mTriangleX, getHeight() + 2);
        canvas.drawPath(mPath, mPaint);
        canvas.restore();
    }

    /*
     * 获取界面的宽度高度
     * */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        TriangleWidth = (int) (w / mVisibleCount * RADIO_TRIANGLE_WIDTH);
        mInitTriangleX = w / 3 / 2 - TriangleWidth / 2;
        TriangleHeight = TriangleWidth / 2;

        initTriangle();
    }

    private void initTriangle() {
        mPath = new Path();
        mPath.moveTo(0, 0);
        mPath.lineTo(TriangleWidth, 0);
        mPath.lineTo(TriangleWidth / 2, -TriangleHeight);
        mPath.close();
    }

    /*
     * 滚动到具体位置
     * */
    public void scroll(int position, float percent) {
        int tabWidth = getWidth() / 3;
        mTriangleX = (int) ((percent + position) * tabWidth);
        invalidate();
    }

    /*
     * 设置可见Tab数量
     * */
    public void setVisibleCount(int mVisibleCount) {
        this.mVisibleCount = mVisibleCount;
    }

    /*
     * 动态添加Tab
     * */
    public void addTabs(ArrayList<String> titles) {
        if (titles != null && titles.size() > 0) {
            this.removeAllViews();

            for (String title : titles) {
                addView(generateTextView(title));
            }
        }
    }

    private View generateTextView(String title) {
        TextView textView = new TextView(getContext());
        textView.setText(title);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(16);
        textView.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        lp.width = getScreenWidth() / mVisibleCount;
        textView.setLayoutParams(lp);

        return textView;
    }

    private int getScreenWidth() {
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }

    /*
    * 暴露方法
    * */
    public interface OnPageChangeListener {
        void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);

        void onPageSelected(int position);

        void onPageScrollStateChanged(int state);
    }

    public void setOnPageChangeListener(OnPageChangeListener listener) {
        this.listener = listener;
    }

    /*
    * tab和ViewPager拼接在一起
    * */
    public void setUpWidthViewPager(ViewPager viewPager)
    {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                scroll(position,positionOffset);

                if(listener!=null)
                {
                  listener.onPageScrolled(position,positionOffset,positionOffsetPixels);
                }
            }

            @Override
            public void onPageSelected(int position) {
                if(listener!=null)
                {
                    listener.onPageSelected(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if(listener!=null)
                {
                    listener.onPageScrollStateChanged(state);
                }
            }
        });
    }
}
